package com.ossean;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.ossean.databaseSource.DBSource;
import com.ossean.databaseSource.GatherDao;
import com.ossean.databaseSource.UpdateControlProjectsDao;
import com.ossean.model.GatherProjectsModel;
import com.ossean.util.MergeProjectNew;
import com.ossean.util.MergeProjectsUtil;

@Component
public class MergeProjects {
	Logger logger = Logger.getLogger(this.getClass());
	@Resource
	private DBSource dbSource;
	@Resource
	private GatherDao gatherDao;
	@Resource
	private UpdateControlProjectsDao updateControlDao;
	
	@Qualifier("mergeProjectNew")
	@Autowired
	private MergeProjectNew mergeProjectNew;
	
	private int startId1;
	
	private static String pointerTableName = "edd_pointers";
	private static String sourceTableName = "gather_projects";
	private static String targetTableName = "edd_relations";
	
	private static int batchSize = 500;
	
	//读指针
    public int readPointer(String table, String source, String target, int minId){
		int pointer = minId;
		try {
			pointer = dbSource.getPointer(table, source, target);
		} catch(Exception e) {
			logger.info("No such pointer! Create one");
			dbSource.insertPointer(table, source, target, pointer);
		}
		return pointer;
	}

	public void start(){
		//int minId = 1;
		//int maxId = dbSource.getMaxId(sourceTableName);
		int count=0;
		count = readPointer(pointerTableName, sourceTableName, targetTableName, count);
		
		while(true){
			//startId1 = readPointer(pointerTableName, sourceTableName, targetTableName, minId);
//			if(startId1 > maxId){
//				
//				try {
//					logger.info("没有数据了，休眠10min");
//					Thread.sleep(600*1000L);
//					continue;
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//				}
//			}
			List<GatherProjectsModel> gpmList1 = gatherDao.selectGPMList(sourceTableName,batchSize);
			
			if(gpmList1.size() == 0){
				try {
					logger.info("no increase item to be handled! handle update items");
					List<GatherProjectsModel> updateProjectList = gatherDao.selectUpdateProjects(sourceTableName,batchSize);
					if(updateProjectList.size() == 0){
						logger.info("all update items have been solved. Sleep 10 mins");
						Thread.sleep(600*1000);
						continue;
				    }
				    else
						for(GatherProjectsModel model:updateProjectList){
							mergeProjectNew.handleNewProject(model, true);
							gatherDao.updateUpdateMark(sourceTableName, 1, model.getId());
						}
					
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			long start = System.currentTimeMillis();
			for(GatherProjectsModel model1:gpmList1){
				logger.info("handling project : " + model1.getId());
				mergeProjectNew.handleNewProject(model1, false);
				count = count+1;
				dbSource.updatePointer(pointerTableName, sourceTableName, targetTableName, count);
				gatherDao.updateUpdateMark(sourceTableName, 1, model1.getId()); //需要更新update_mark字段 因为按顺序取的时候可能取到1的
			}
			long end = System.currentTimeMillis();
			logger.warn("deal with: 500 projects cost: "+(float)(end - start)/60000+" minutes");
		}
	}
	
	
	public static void main(String[] args){
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/applicationContext*.xml");
		MergeProjects Main = applicationContext.getBean(MergeProjects.class);
		Main.start();
	}
}
